﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DarwinIsDead {
    class ArenaComponent : DrawableGameComponent {
        public World World { get; private set; }
        public bool UpdateEnabled { get; set; }
        public bool DrawEnabled { get; set; }

        private SpriteBatch spriteBatch;
        private SpriteFont debugFont;

        private Player player;
        private ZombieFactory zombieFactory;

        private bool retry;
        private bool retrySelect;
        private bool retryPressed;
        private string retryPrompt;

        public ArenaComponent(Game game) : base(game) {
            World = new World(game);
            UpdateEnabled = true;
            DrawEnabled = true;
            retry = false;
            retryPrompt = "";
            retryPressed = false;
            retrySelect = true;

            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            player = new Player(World, 320.0f, 240.0f);
            player.OnDeath += OnPlayerDeath;

            zombieFactory = new ZombieFactory(World, player, 3, 8, 0.5f);
        }

        public override void Initialize() {
            debugFont = Game.Content.Load<SpriteFont>("Fonts\\Courier New");            

            World.AddEntity(player);

            // Register traits to zombie factory
            zombieFactory.RegisterTrait(new Traits.RandomBurst(null, World));
            zombieFactory.RegisterTrait(new Traits.Shield(null, World));
            zombieFactory.RegisterTrait(new Traits.UsainBolt(null, World));
            zombieFactory.RegisterTrait(new Traits.Arm(null, World));
            zombieFactory.RegisterTrait(new Traits.DirectMovement(null, World));
            zombieFactory.RegisterTrait(new Traits.Elusion(null, World));
            zombieFactory.RegisterTrait(new Traits.Frenzy(null, World));
            zombieFactory.RegisterTrait(new Traits.SprintTrait(null, World));
            zombieFactory.RegisterTrait(new Traits.Transparency(null, World));
            zombieFactory.RegisterTrait(new Traits.TimeLordTrait(null, World));
            zombieFactory.RegisterTrait(new Traits.SlowHeal(null, World));
            zombieFactory.RegisterTrait(new Traits.AbsorbShot(
                World, new Vector2(10f, 10f), 500f, 6, 1.1f, 1000f,
                1.2f));

            Traits.StraightShooter sharkShooter = new Traits.StraightShooter(World, new Vector2(24f, 24f), 3, 0.25f, 4f);
            sharkShooter.Sprite = World.Game.Content.Load<Texture2D>("Sprites\\Traits\\sprSharkHead");
            for (int i = 0; i < 3; i++) {
                Bullet bullet = new Bullets.SharkTooth(World, false, 4);
                sharkShooter.Bullets.Add(bullet);
                World.AddEntity(bullet);
            }
            sharkShooter.HealthBonus = 15;

            //zombieFactory.RegisterTrait(sharkShooter);
            zombieFactory.InitialWave();
        }

        public override void Update(GameTime gameTime) {
            if (UpdateEnabled) {
                if (retry) {
                    KeyboardState keyState = Keyboard.GetState();
                    if (!retryPressed && (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.Right))) {
                        retrySelect = !retrySelect;
                        retryPressed = true;
                    } else if (keyState.IsKeyUp(Keys.Left) && keyState.IsKeyUp(Keys.Right)) {
                        retryPressed = false;
                    }

                    if (retrySelect) {
                        retryPrompt = ">Yes       No";
                    } else {
                        retryPrompt = " Yes      >No";
                    }

                    if (keyState.IsKeyDown(Keys.Enter)) {
                        if (retrySelect) {
                            player.Health = 100;
                            player.Alive = true;
                            player.Solid = true;
                            retry = false;

                            zombieFactory.Reset();
                            zombieFactory.InitialWave();
                        } else {
                            Game.Exit();
                        }
                    }
                }
                World.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime) {
            if (DrawEnabled) {

                World.Draw();

                Vector2 mp = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

                spriteBatch.Begin();
                spriteBatch.DrawString(debugFont, "+", mp, Color.Red);

                spriteBatch.DrawString(debugFont, "Zombies Left: " +
                    (zombieFactory.NumZombies - zombieFactory.DeadZombies),
                    new Vector2(400f, 0f), Color.White);

                if (retry) {
                    Random rng = new Random();
                    Color col = new Color(rng.Next(255), rng.Next(255), rng.Next(255));
                    spriteBatch.DrawString(debugFont, "Retry?", new Vector2(320f, 240f), col);
                    spriteBatch.DrawString(debugFont, retryPrompt, new Vector2(320f, 300f), col);
                }
                spriteBatch.End();
            }
        }

        public void OnPlayerDeath() {
            retry = true;
        }
    }
}
